﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Solver;
using Parser;

namespace ChisMetLab5
{
  public partial class Right : Form
  {
    public Right()
    {
      InitializeComponent();
    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
      if (radioButton2.Checked)
      {
        pictureBox1.Load("test1.png");
      }
    }

    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
      if (radioButton3.Checked)
      {
        pictureBox1.Load("test2.png");
      }
    }

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {
      if (radioButton1.Checked)
      {
        pictureBox1.Load("major.png");
      }
    }

    private void button1_Click(object sender, EventArgs e)
    {
      dataGridView1.Rows.Clear();
      Solver.Solver s;
      s = new Solver.Solver();
      int n, m;
      double lx, rx, ry, ly;
      int Smax = Convert.ToInt32(textBox5.Text);
      double EpsMin = Convert.ToDouble(textBox4.Text);

      //Входные параметры
      n = Convert.ToInt32(textBox1.Text);
      m = Convert.ToInt32(textBox2.Text);
      lx = -1.0;
      rx = 1.0;
      ry = 1.0;
      ly = -1.0;

      // Определяем тип задачи
      int type = 1;
      if (radioButton1.Checked)
      {
        type = 1;
      }
      if (radioButton2.Checked)
      {
        type = 0;
      }
      if (radioButton3.Checked)
      {
        type = -1;
      }

      // Передаём параметры в солвер
      s.prepareParams(n, m, Smax, EpsMin, lx, ly, rx, ry, type);

      #region Проверить и по необходимости взять из парсера ГУ
      if (checkBox3.Checked)
      {
        String left, right, top, bottom;
        left = Left.Text + 'F';
        right = Right1.Text + 'F';
        top = Top.Text + 'F';
        bottom = Bottom.Text + 'F';

        // 
        Parser.Parser l, r, t, b;
        l = new Parser.Parser();
        r = new Parser.Parser();
        t = new Parser.Parser();
        b = new Parser.Parser();
        l.Parse(left.ToCharArray(), left.Length);
        r.Parse(right.ToCharArray(), right.Length);
        t.Parse(top.ToCharArray(), top.Length);
        b.Parse(bottom.ToCharArray(), bottom.Length);

        double h = (rx - lx) / n;
        double dt = (ry - ly) / m;
        double epsMax = -1.0;

        #region запись ГУ в солвер
        // Нижняя 
        for (int i = 0; i <= n; i++)
        {

          s.setX0(i, lx + h * i);
        }

        // Верхняя
        for (int i = 0; i <= n; i++)
        {

          s.setXM(i, lx + h * i);
        }

        // Левая
        for (int j = 0; j <= m; j++)
        {

          s.set0Y(j, ly + h * j);
        }

        // Правая
        for (int j = 0; j <= m; j++)
        {

          s.setNY(j, ly + h * j);
        }
        #endregion
      }

      #endregion

      s.solve(type);


      // Выводить график:
      if (checkBox2.Checked)
      {
        //userControl11.surf = s.getValueV;
        //userControl11.n = n;
        //userControl11.startDraw();
      }

      #region Вычисление EpsMax
      if (type != 1)
      {
        double h = (rx - lx) / n;
        double dt = (ry - ly) / m;
        double epsMax = -1.0;
        for (int j = 0; j <= m; j++)
        {
          for (int i = 0; i <= n; i++)
          {
            double u, v;
            u = s.getTestSolution(lx + i * h, ly + j * dt, type);
            v = v = s.getValueV(i, j);
            if (Math.Abs(u - v) > epsMax)
            {
              epsMax = Math.Abs(u - v);
            }
          }
        }
        textBox3.Text = epsMax.ToString("E2");
        textBox6.Text = s.getS().ToString("G6");
        textBox7.Text = s.getEps().ToString("E2");
      }
      #endregion
      #region вывод таблиц
      if (checkBox2.Checked)
      {
        double h = (rx - lx) / n;
        double dt = (ry - ly) / m;
        int rowsCount = 0;
        for (int j = 0; j <= m; j++)
        {
          for (int i = 0; i <= n; i++)
          {
            double u = -1.0, v;
            v = s.getValueV(i, j);
            if (type != 1)
            {
              u = s.getTestSolution(lx + i * h, ly + j * dt, type);
            }
            dataGridView1.Rows.Add();
            dataGridView1.Rows[rowsCount].Cells[0].Value = j.ToString("G6");
            dataGridView1.Rows[rowsCount].Cells[1].Value = i.ToString("G6");
            dataGridView1.Rows[rowsCount].Cells[2].Value = v.ToString("E2");
            if (type != 1)
            {
              dataGridView1.Rows[rowsCount].Cells[3].Value = u.ToString("E2");
            }
            else
            {
              dataGridView1.Rows[rowsCount].Cells[3].Value = "N / A";
            }
            if (type != 1)
            {
              dataGridView1.Rows[rowsCount].Cells[4].Value = Math.Abs(u - v).ToString("E2");
            }
            rowsCount++;
          }
        }
      }
      #endregion

    }
  }
}
